<template>
	<view class="touxiang-div">
		<image   :src="path"  style="width: 100%;height: 700rpx;"  @tap="uploade"></image>
	</view>
</template>

<script>
    import httpPath from '@/static/js/path.js';
	import {getlogin} from '@/static/js/login.js';
	export default {
		data() {
			return {
				user:{},//用户登录信息
				path:"/static/img/touxiang.png"
			}
		},
	    onPageScroll(e) {
	    	//兼容iOS端下拉时顶部漂移
	    	this.headerPosition = e.scrollTop>=0?"fixed":"absolute";
	    	this.headerTop = e.scrollTop>=0?null:0;
	    	this.statusTop = e.scrollTop>=0?null:-this.statusHeight+'px'; 
	    },
		onLoad() {
	       this.user = getlogin();
	       if(!this.user.islogin){
	       	uni.showModal({
	       	   showCancel:false,
	       	   content:"您还没有登录，请登录"
	       	})
	       	uni.navigateTo({
	       		url:"../public/login"
	       	})
	       }
		   this.getdata()
		},
		onNavigationBarButtonTap(){
			this.upfile()
		},
		methods: {
		   getdata(){//加载头像
			   uni.request({
				   url: httpPath.filePath+'api.php/index/member/member_info', //仅为示例，并非真实接口地址。
				   method:'POST',
				   header:{'content-type':'application/x-www-form-urlencoded'},
				   data: {member_id:this.user.member_id},
				   success: (res) => {
					   if(res.data.code && res.data.code==200){
						   if(res.data.result.headpic){
							    this.path=res.data.result.headpic;
						   }
						  
					   }
				   }
			   });
			},
	        uploade(){
				let _this=this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType:['album','camera'], //从相册选择
					success: function (res) {
					    _this.path= res.tempFilePaths[0]	
						
					}
				})
			},upfile(){
				let _this=this;
				let memberid = _this.user.member_id+""
				uni.uploadFile({
					url:httpPath.filePath+'/api.php/index/member/member_avatar', //仅为示例，非真实的接口地址
					filePath:_this.path,
					name: 'member_avatar',
					formData: {
						'member_id':memberid
					},
					success: (uploadFileRes) => {	
						let data=eval("("+uploadFileRes.data+")");
						console.log(uploadFileRes)
						if(data.code=="200"){
							uni.showToast({
								title:'头像上传成功'
							})
							uni.navigateBack()
						}
					}
				});
			}
		}
	}
</script>

<style>
	@import "../../colorui/main.css";
	.touxiang-div{padding-top: 30%;}
	.touxiang{width: 100%;height: 700upx;}
</style>
